package tree.递归;

import po.TreeNode;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 二叉树的堂兄弟节点993.java
 * @Description https://leetcode-cn.com/problems/cousins-in-binary-tree/
 * @createTime 2021年08月23日 19:05:00
 */
public class 二叉树的堂兄弟节点993 {
	int x,y;
	int deepx=0,deepy=0;
	TreeNode parentX,parentY;
	boolean findX=false,findY=false;

	public boolean isCousins(TreeNode root, int x, int y) {
		this.x=x;
		this.y=y;
		dfs(root,0,null);
		return deepx==deepy&&parentX!=parentY;
	}

	private void dfs(TreeNode node, int deep, TreeNode parent) {
		if (node == null) {
			return;
		}
		if (node.val==x){
			deepx=deep;
			parentX=parent;
			findX=true;
		}
		if (node.val == y) {
			deepy=deep;
			parentY=parent;
			findY=true;
		}
		if (findY && findX) {
			return;
		}
		dfs(node.left,deep+1,node);
		if (findY && findX) {
			return;
		}
		dfs(node.right,deep+1,node);
	}
}
